﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Drawing;
using Timer = System.Timers.Timer;
using System.Diagnostics;
using System.Collections.ObjectModel;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using Window = System.Windows.Window;
using System.Collections.Concurrent;
using System.Net.NetworkInformation;
using static NPOI.HSSF.Util.HSSFColor;
using bwtSocket;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using ProgressBar = System.Windows.Controls.ProgressBar;
using Org.BouncyCastle.Utilities;
using NPOI.SS.Formula.Functions;
using static CosAging.PublicClass;
using System.Threading;

namespace CosAging
{

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private double WINLeft, WINTop, WINWidth, WINHeight;
        private bool MAXWindow = false;
        public static bool IsTcpConnectTesting = false;
        public double PixelCoefficient;
        public Timer StartTimer = new Timer();
        public Timer EQPStateTimer = new Timer();
        public Timer CountDownTimer = new Timer();
        public bool CountDownTimerIsRunning = false;
        public bool Startlisten = false;
        //public static Grating_Interface.View.SystemMonitor ViewSystemMonitor = new Grating_Interface.View.SystemMonitor();
        public static CosAging.View.SystemMonitor2 ViewSystemMonitor2 = new CosAging.View.SystemMonitor2();
        public static CosAging.View.SystemMonitor1 ViewSystemMonitor1 = new CosAging.View.SystemMonitor1();
        public static CosAging.View.SystemSetting ViewSystemSetting = new CosAging.View.SystemSetting();
        //public static CosAging.View.SystemMonitor3 ViewSystemMonitor3 = new CosAging.View.SystemMonitor3();
        //public static Grating_Interface.View.SystemMonitor3 ViewSystemMonitor3 = new Grating_Interface.View.SystemMonitor3();

        public static bool 是否继续操作 = false;
        public static bool IsCameraWindowShow = false,IsNewportWindowShow = false;
        public static bool Systemrunning = true;
        public MainWindow()
        {
            //PublicClass.计时起点();
            InitializeComponent();
            this.StateChanged += new EventHandler(Window_StateChanged);
            this.MaxHeight = SystemParameters.PrimaryScreenHeight;
            Class_Initialize(); 
            TimerSetting();
            //PublicClass.计时终点();
            //PublicClass.Logger.Info(PublicClass.时间差);

        }

        private void Class_Initialize()
        {
            try
            {
                PublicClass.Loginstate.stringvalue = "未登录";
                登录状态.DataContext = PublicClass.Loginstate;
                北京时间.DataContext = PublicClass.Loginstate;
                //myIniFile.iniFileName = Apppath + INISYS;
                //SQL.Mysqlreadtablestructure();
                //MultipleDemo MD = new MultipleDemo();
                //MD.Show();
                //AccessDB.Access_Read();
                //DeviceHistoryInitialize();
                //StatePercentInitialize();
            }
            catch (Exception ex)
            {
                NLOGex(ex);
            }
        }
        private void TimerSetting()
        {
            //多线程，先计时等一秒
            StartTimer.AutoReset = false;
            StartTimer.Interval = 1000;
            StartTimer.Enabled = true;
            StartTimer.Elapsed += StartTimerEvent;      
        }
        private void StartTimerEvent(object sender, EventArgs e)
        {

            //EQPStateTimer.Interval = TimeSpan.FromMilliseconds(10);    
            //EQPStateTimer.Tick+=EQPStateEvent;
            //EQPStateTimer.Start();
            CountDownTimer.AutoReset = false;
            CountDownTimer.Interval = 1000;
            CountDownTimer.Enabled = false;
            CountDownTimer.Elapsed += CountDownEvent;

            EQPStateTimer.AutoReset = false;
            EQPStateTimer.Interval = 100;
            EQPStateTimer.Elapsed += EQPStateEvent;
            EQPStateTimer.Enabled = true;

        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // 设置居中
            //System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            // 设置全屏
            //this.WindowState = System.Windows.WindowState.Normal;
            //this.WindowStyle = System.Windows.WindowStyle.None;
            //this.ResizeMode = System.Windows.ResizeMode.NoResize;
            //this.Topmost = false;

            this.Width = 1410;
            this.Height = 800;

            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            // 获得DPI，像素系数
            Graphics g = Graphics.FromHwnd(IntPtr.Zero);
            PixelCoefficient = 96 / g.DpiX;
            //↓↓↓↓↓↓↓↓↓↓↓↓↓↓CDSDTint转绑定到CDSDTstr↓↓↓↓↓↓↓↓↓↓↓↓↓↓
            //foreach (var BT in PublicClass.CDSDTstr)      //界面中所有控件遍历
            //{
       
            //}
            //↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

        }

        private void mainform_FormClosing(System.Object eventSender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                string message = "您确认要退出程序？";
                string caption = "警告";
                MessageBoxButton buttons = MessageBoxButton.YesNo;
                MessageBoxImage icon = MessageBoxImage.Information;
                // Show message box
                MessageBoxResult result = MessageBox.Show(message, caption, buttons, icon);

                if (result == MessageBoxResult.No)  // 取消退出操作
                {
                    e.Cancel = true;
                }
                else if (result == MessageBoxResult.Yes)
                {
                    //StopServerToolStripMenuItem();
                    //StopServer();
                    Systemrunning = false;
                    EQPStateTimer.Enabled = false;
                    e.Cancel = false;
                    System.Windows.Application.Current.Shutdown();
                }
                //eventArgs.Cancel = Cancel;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
            }

        }

        private void MainWindows_Keydown(object sender, KeyEventArgs e)
        {
            //PublicClass.KeyboardandShutdown(sender, e);
        }
        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
                this.DragMove();
        }
        private void WindowClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
            //EQPStateTimer.Enabled = false;
            //Application.Current.Shutdown();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

        }

        private void WINMAX_Click(Object sender, RoutedEventArgs e)
        {

            //this.WindowState = WindowState.Maximized;

            if (MAXWindow == false)
            {
                WINLeft = this.Left;
                WINTop = this.Top;
                WINWidth = this.Width;
                WINHeight = this.Height;
                Rect rc = SystemParameters.WorkArea; //获取工作区大小  
                this.Left = 0; //设置位置  
                this.Top = 0;
                this.Width = rc.Width;
                this.Height = rc.Height;
                MAXWindow = true;

            }
            else
            {
                this.Left = WINLeft;
                this.Top = WINTop;
                this.Width = WINWidth;
                this.Height = WINHeight;
                MAXWindow = false;
            }
        }
        private void WINMIN_Click(Object sender, RoutedEventArgs e)
        {

            this.WindowState = WindowState.Minimized;

        }
        private void Menu_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton CRadioButton = (RadioButton)sender;
            switch (CRadioButton.Name)
            {
                case "RB_SystemMonitor1":
                    Main_ContentControl.Content = ViewSystemMonitor1;                
                    break;

                case "RB_SystemMonitor2":
                    Main_ContentControl.Content = ViewSystemMonitor2;
                    break; 

                case "RB_SystemSetting":
                    Main_ContentControl.Content = ViewSystemSetting;
                    break;

                case "RB_SystemMonitor3":
                    //Main_ContentControl.Content = ViewSystemMonitor3;
                    break;
                default:
                    //Console.WriteLine("Default case");
                    break;
            }
        }


        private void Loginevent(object sender, RoutedEventArgs e)
        {
            if (PublicClass.Loginstate.stringvalue == "未登录")
            {
                CosAging.Windows.login lg = new Windows.login();
                lg.ShowDialog();
            }
            else
            {
                string message = "是否登出当前用户？";
                string caption = "警告";
                MessageBoxButton buttons = MessageBoxButton.YesNo;
                MessageBoxImage icon = MessageBoxImage.Information;
                // Show message box
                MessageBoxResult result = System.Windows.MessageBox.Show(message, caption, buttons, icon);

                if (result == MessageBoxResult.No)  // 取消退出操作
                {
                    return;
                }
                else if (result == MessageBoxResult.Yes)
                {
                    PublicClass.Loginstate.stringvalue = "未登录"; PublicClass.CosBobject.enable = false;
                }
            }
        }
        private void Cameraevent(object sender, RoutedEventArgs e)
        {
            //MultipleDemo MD=new MultipleDemo();
            //MD.Show();
            if (IsCameraWindowShow == false)
            {
                CameraWindow CW = new CameraWindow();
                double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
                double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
                double windowWidth = CW.Width;
                double windowHeight = CW.Height;
                CW.Left = (screenWidth / 2) - (windowWidth / 2);
                CW.Top = (screenHeight / 2) - (windowHeight / 2);
                CW.Show();
                IsCameraWindowShow = true;
            }
            else
            {
                MessageBox.Show("摄像机串口窗已打开");
            }
        }
        private void Newportevent(object sender, RoutedEventArgs e)
        {

        }

        private void Window_MouseMove(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }
        private void EQPStateEvent(object sender, EventArgs e)
        {
            try
            {
                while(true) 
                {
                   Loginstate.beijingtime = DateTime.Now.ToString();
                   Thread.Sleep(200);
                }

                //EQPStateTimer.Enabled = false  ;
                //if (CountDownTimerIsRunning == false)
                //{
                //    CountDownTimer.Enabled = true;
                //}
                //EQPStateTimer.Enabled = true;
            }
            catch (Exception ex)
            {
                NLOGex(ex);
            }
        }

        private void CountDownEvent(object sender, EventArgs e)
        {
            try
            {
                CountDownTimer.Enabled = false;
                CountDownTimerIsRunning = true;
                CountDownTimerIsRunning = false;
            }
            catch (Exception ex)
            {
                NLOGex(ex);
            }
        }

            
        /// <summary>
        /// 处理接收到客户端的请求和数据
        /// </summary>
        /// <param name="sks"></param>

        private void Window_StateChanged(object sender, EventArgs e)
        {

            //if (this.WindowState == WindowState.Minimized)
            //{

            //    //string message = "是否最小化到托盘";
            //    //string caption = "请确认";
            //    //MessageBoxButton buttons = MessageBoxButton.YesNo;
            //    //MessageBoxImage icon = MessageBoxImage.Information;
            //    //// Show message box
            //    //MessageBoxResult result = MessageBox.Show(message, caption, buttons, icon);

            //    //if (result == MessageBoxResult.Yes)
            //    //{
            //    //    this.Hide();
            //    //}
    

            //    //this.ShowInTaskbar = true;
            //    //this.Hide();
            //    //this.notfyicon.Visibility = Visibility.Visible;
            //}

            //if (this.WindowState == WindowState.Normal || this.WindowState == WindowState.Maximized)
            //{
            //    this.Show();
            //    this.Activate();
            //}

        }



        //private void MenuItemLeave_Click(object sender, EventArgs e)
        //{
        //    MessageBox.Show("提示离线！");
        //}
        //private void MenuItemNoBother_Click(object sender, EventArgs e)
        //{
        //    MessageBox.Show("提示请勿打扰！");
        //}
        //private void MenuItemBusy_Click(object sender, EventArgs e)
        //{
        //    MessageBox.Show("提示在忙！");
        //}
        //private void MenuItemHidden_Click(object sender, EventArgs e)
        //{
        //    MessageBox.Show("提示隐身！");
        //}
        //private void MenuItemOffline_Click(object sender, EventArgs e)
        //{
        //    MessageBox.Show("提示暂时离开！");
        //}
        //private void MenuItemAboutUs_Click(object sender, EventArgs e)
        //{
        //    MessageBox.Show("提示关于我们！");

        //}
        //private void MenuIteOnline_Click(object sender, EventArgs e)
        //{
        //    MessageBox.Show("提示上线！");
        //}
        private void MenuItemExit_Click(object sender, EventArgs e)
        {

            this.Close();

        }
    }
}
